#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int res = 0;

        unordered_map<int, int> hash;

        for (int left = 0, right = 0; right < fruits.size(); right++)
        {
            hash[fruits[right]]++;

            while (hash.size() > 2)
            {
                hash[fruits[left]]--;
                if (hash[fruits[left]] == 0)
                    hash.erase(fruits[left]);
                left++;
            }

            res = max(res, right - left + 1);
        }
        return res;

    }
};